package com.ce.pms.house.service.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.charge.dao.IPeriodDao;
import com.ce.pms.charge.model.PeriodDomain;
import com.ce.pms.common.annotation.NoDACL;
import com.ce.pms.common.annotation.PrecinctQuery;
import com.ce.pms.common.model.SysInfo;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.dao.IBuildingDao;
import com.ce.pms.house.dao.IChargePrecinctDao;
import com.ce.pms.house.dao.IManageAreaDao;
import com.ce.pms.house.dao.IManageOrgDao;
import com.ce.pms.house.model.BuildingDomain;
import com.ce.pms.house.model.ChargePrecinctDomain;
import com.ce.pms.house.model.CompanyDomain;
import com.ce.pms.house.model.ManageOrgDomain;
import com.ce.pms.house.service.IBuildingService;
import com.ce.pms.house.service.IManageAreaService;
import com.ce.pms.house.vo.BuildingVO;
import com.ce.pms.house.vo.CompanyVO;
import com.ce.pms.house.vo.ManageOrgVO;
import com.ce.pms.house.vo.PrecinctVO;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;
import com.sitechasia.webx.core.support.Page;
import com.sitechasia.webx.core.utils.base.PopulateUtil;

/**
 * 功能描述: 管理区域设置Service实现类
 * <p>
 * 版权所有：中企动力
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author 胡煦 Create on:2008-7-8 Edit author: Edit on: 修改原因：
 *
 */

public class ManageAreaServiceImpl extends PmsBaseServiceImpl implements
		IManageAreaService {

	/**
	 * 方法描述: 查询所有收费管理区
	 * @author 胡煦
	 * Create on:2008-8-28
	 * @param
	 * @return List<PrecinctVO>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	@PrecinctQuery(alias="precinct", beginStr=" where ", endStr=" ", insertAfterPoint="precinct", property4PrecinctId="keyword")
	public List<PrecinctVO> getAllChargePrecinct(){
		List<ChargePrecinctDomain> ChargePrecinctDomainList = chargePrecinctDao.findAllResult("from ChargePrecinctDomain precinct", null);;
		List<PrecinctVO> ChargePrecinctVOList = new ArrayList<PrecinctVO>();
		for(ChargePrecinctDomain chargePrecinctDomain : ChargePrecinctDomainList){
			PrecinctVO chargePrecinctVO = this.assembleD2V(chargePrecinctDomain);
			ChargePrecinctVOList.add(chargePrecinctVO);
		}
		return ChargePrecinctVOList;
	}

	/**
	 * 方法描述: 查找所有物业公司基本资料
	 *
	 * @author 胡煦 Create on:2008-7-9
	 * @param companyVO
	 * @return companyVO Edit author: Edit on: 修改原因：
	 */
	public List<CompanyVO> findAllCompany() {

		List<CompanyVO> list = new ArrayList<CompanyVO>();
		List<CompanyDomain> CompanyDomainList = manageAreaDao.findAll();

		for (CompanyDomain companyDomain : CompanyDomainList) {
			CompanyVO companyVO = this.assembleD2V(companyDomain);
			list.add(companyVO);
		}

		return list;
	}

	/**
	 * 功能描述:根据管理处名字查询记录数<br>
	 * 注：新建判断name的唯一性时，oldName传为null；修改判断name的唯一性时，则需要传入修改前的名字<br>
	 *
	 * @param manageOrgByName
	 * @param oldName
	 * @return Create author:胡煦 Create on:2008-7-14 Edit author: Edit on: Why:
	 */
	public Long checkManageOrgByName(String manageOrgByName, String oldName) {
		return manageOrgDao.checkManageOrgByName(manageOrgByName, oldName);
	}

	/**
	 * 功能描述:根据管理区名字查询记录数<br>
	 * 注：新建判断name的唯一性时，oldName传为null；修改判断name的唯一性时，则需要传入修改前的名字<br>
	 *
	 * @param precinctName
	 * @param oldName
	 * @return Create author:胡煦 Create on:2008-7-14 Edit author: Edit on: Why:
	 */
	public Long checkPrecinctByName(String precinctName, String oldName) {
		return chargePrecinctDao.checkPrecinctByName(precinctName, oldName);
	}

	/**
	 * 功能描述:根据管理区编号查询记录数<br>
	 * 注：新建判断管理区编号的唯一性时，oldCode传为null；修改判断管理区编号的唯一性时，则需要传入修改前的code<br>
	 * @param precinctCode
	 * @param oldCode
	 * @return
	 * Create author:胡煦
	 * Create on:2008-9-17
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public Long checkPrecinctByCode(String precinctCode,String oldCode) {
		return chargePrecinctDao.checkPrecinctByCode(precinctCode, oldCode);
	}

	/**
	 * 方法描述: 根据管理区Id查询管理区VO
	 *
	 * @author 胡煦 Create on:2008-7-9
	 * @param precinctId
	 * @return PrecinctVO Edit author: Edit on: 修改原因：
	 */
	public PrecinctVO findPrecinctById(Long precinctId) {
		super.checkParameterIsNull(precinctId,
				"common:parameters_null_exception, ");
		return this.assembleD2V(chargePrecinctDao.findById(precinctId));
	}

	/**
	 * 方法描述: 根据物业管理处Id查询物业管理处VO
	 *
	 * @author 胡煦 Create on:2008-7-9
	 * @param manageOrgId
	 * @return ManageOrgVO Edit author: Edit on: 修改原因：
	 */
	public ManageOrgVO findManageOrgById(Long manageOrgId) {
		super.checkParameterIsNull(manageOrgId,
				"common:parameters_null_exception, ");
		return this.assembleD2V(manageOrgDao.findById(manageOrgId));
	}

	/**
	 * 方法描述: 修改物业公司基本资料
	 *
	 * @author 胡煦 Create on:2008-7-9
	 * @param companyVO
	 * @return companyVO Edit author: Edit on: 修改原因：
	 */
	public CompanyVO editCompany(CompanyVO companyVO) {

		super.checkParameterIsNull(companyVO,
				"common:parameters_null_exception, ");

		CompanyDomain companyDomain = manageAreaDao.findById(companyVO
				.getKeyword());
		PopulateUtil.populate(companyVO, companyDomain);

		SysInfo sysInfo = createInfo(companyVO.getCreator());
		modifyInfo(sysInfo, companyVO.getModifier());
		companyDomain.setSysInfo(sysInfo);

		companyDomain.setQualify(companyVO
				.getQualifyId()==null?null:sysrefcodeDao.findById(companyVO
				.getQualifyId()));

		PopulateUtil.populate(companyDomain, companyVO);
		return companyVO;
	}

	/**
	 * 方法描述: 新建管理处，必须基于某个物业公司
	 *
	 * @author 胡煦 Create on:2008-7-9
	 * @param ManageOrgVO
	 * @return ManageOrgVO Edit author: Edit on: 修改原因：
	 */
	public ManageOrgVO addManageOrg(ManageOrgVO manageOrgVO) {

		super.checkParameterIsNull(manageOrgVO,
				"common:parameters_null_exception, ");

		if (checkManageOrgByName(manageOrgVO.getName(), null) > 0) {
			throw new BusinessException(
					"house:manageOrgVO_name_exist_exception",
					BusinessException.WARNING);
		}
		ManageOrgDomain manageOrgDomain = new ManageOrgDomain();
		PopulateUtil.populate(manageOrgVO, manageOrgDomain);

		manageOrgDomain.setCompany(manageAreaDao.findById(manageOrgVO
				.getCompanyId()));

		SysInfo sysInfo = createInfo(manageOrgVO.getCreator());
		manageOrgDomain.setSysInfo(sysInfo);

		manageOrgDao.saveObject(manageOrgDomain);

		return this.assembleD2V(manageOrgDomain);
	}

	/**
	 * 方法描述: 修改管理处，必须基于某个物业公司
	 *
	 * @author 胡煦 Create on:2008-7-9
	 * @param ManageOrgVO
	 * @return ManageOrgVO Edit author: Edit on: 修改原因：
	 */
	public ManageOrgVO editManageOrg(ManageOrgVO manageOrgVO) {
		super.checkParameterIsNull(manageOrgVO,
				"common:parameters_null_exception, ");

		if (checkManageOrgByName(manageOrgVO.getName(), manageOrgVO
				.getOldName()) > 0) {
			throw new BusinessException(
					"house:manageOrgVO_name_exist_exception",
					BusinessException.WARNING);
		}

		ManageOrgDomain manageOrgDomain = manageOrgDao.findById(manageOrgVO
				.getKeyword());
		PopulateUtil.populate(manageOrgVO, manageOrgDomain);

		SysInfo sysInfo = createInfo(manageOrgVO.getCreator());
		this.modifyInfo(sysInfo, manageOrgVO.getModifier());
		manageOrgDomain.setSysInfo(sysInfo);

		return this.assembleD2V(manageOrgDomain);
	}

	/**
	 * 方法描述: 根据管理处ID分页查询其下所有管理区
	 *
	 * @author 胡煦 Create on:2008-7-3
	 * @param qc
	 * @param pageNo
	 * @param pageSize
	 * @return page Edit author: Edit on: 修改原因：
	 */
	public Page queryPagePrecinct(Long id, int pageNo, int pageSize) {
		super.checkParameterIsNull(id, "common:parameters_null_exception, ");
		Page page = chargePrecinctDao.queryPagePrecinct(id, pageNo, pageSize);

		List<ChargePrecinctDomain> list = page.getResult();
		List<PrecinctVO> precinctVOList = new ArrayList<PrecinctVO>();

		for (ChargePrecinctDomain chargePrecinctDomain : list) {
			PrecinctVO precinctVO = assembleD2V(chargePrecinctDomain);
			precinctVOList.add(precinctVO);
		}

		page.setResult(precinctVOList);
		return page;
	}

	/**
	 * 方法描述: 新建管理区，必须基于某个管理处
	 *
	 * @author 胡煦 Create on:2008-7-9
	 * @param precinctVO
	 * @return precinctVO Edit author: Edit on: 修改原因：
	 */
	public PrecinctVO addPrecinct(PrecinctVO precinctVO) {

		super.checkParameterIsNull(precinctVO,
				"common:parameters_null_exception, ");

		if (checkPrecinctByName(precinctVO.getName(), null) > 0) {
			throw new BusinessException("house:precinct_name_exist_exception",
					BusinessException.WARNING);
		}

		ChargePrecinctDomain chargePrecinctDomain = assembleV2D(precinctVO);
		chargePrecinctDao.saveObject(chargePrecinctDomain);
		precinctVO.setKeyword(chargePrecinctDomain.getKeyword());
		try {
			this.addPeriodBy(chargePrecinctDomain, precinctVO.getPeriod());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return precinctVO;
	}

	/**
	 * 方法描述: 增加管理区对应的收费周期
	 * @author 胡煦
	 * Create on:2008-9-17
	 * @param nameStr：查询条件班级名称
	 * @return 返回当前页的查询结果
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 * @throws ParseException
	 */
	private PeriodDomain addPeriodBy(ChargePrecinctDomain chargePrecinctDomain,String periodDate) throws ParseException{
		PeriodDomain periodDomain = new PeriodDomain();
//		String[] dates=periodDate.split("-");
//		String period = new String(dates[0]+dates[1]);
		periodDomain.setPeriod(periodDate);
		periodDomain.setChargePrecinct(chargePrecinctDomain);
		periodDomain.setSysInfo(chargePrecinctDomain.getSysInfo());
		periodDomain.setValidity(SysrefcodeDO.CONSTANTS_DIGITAL_ONE);

		String dateStr = periodDate.substring(0, 4)+ "-" + periodDate.substring(4, 5);
		periodDomain.setStartDate(DateUtil.parseStrToDate(DateUtil.getFirstDayOfMonthofYear(dateStr)));
		periodDomain.setEndDate(DateUtil.parseStrToDate(DateUtil.getLastDayOfMonthOfYear(dateStr)));
		return periodDao.saveObject(periodDomain);
	}


	/**
	 * 方法描述: 修改管理区
	 *
	 * @author 胡煦 Create on:2008-7-9
	 * @param precinctVO
	 * @return precinctVO Edit author: Edit on: 修改原因：
	 */
	public PrecinctVO editPrecinct(PrecinctVO precinctVO) {

		super.checkParameterIsNull(precinctVO,
				"common:parameters_null_exception, ");

		if (checkPrecinctByName(precinctVO.getName(), precinctVO.getOldName()) > 0) {
			throw new BusinessException("house:precinct_name_exist_exception",
					BusinessException.WARNING);
		}

		chargePrecinctDao.saveOrUpdateObject(assembleV2D(precinctVO));

		return precinctVO;
	}

	/**
	 * 方法描述: 删除管理区
	 *
	 * @author 胡煦 Create on:2008-7-9
	 * @param ManageOrgVO
	 * @return null Edit author: Edit on: 修改原因：
	 */
	public void deletePrecinct(Long precinctId) {
		super.checkParameterIsNull(precinctId,
				"common:parameters_null_exception, ");
		this.checkPrecinctIsHas(precinctId);
		List<PeriodDomain> periodDos = periodDao.findPeriodByPrecinct(precinctId, null);
		for(PeriodDomain periodDomain : periodDos){
			periodDao.deleteObject(periodDomain);
		}
		chargePrecinctDao.deleteObjectById(precinctId);
	}

	/**
	 * 方法描述: 删除管理处，必须基于某个物业公司
	 *
	 * @author 胡煦 Create on:2008-7-9
	 * @param ManageOrgVO
	 * @return null Edit author: Edit on: 修改原因：
	 */
	public void deleteManageOrg(Long manageOrgId) {
		super.checkParameterIsNull(manageOrgId,
				"common:parameters_null_exception, ");
		manageOrgDao.deleteObjectById(manageOrgId);
	}

	/**
	 * 方法描述: 根据物业公司Id查询物业公司VO
	 *
	 * @author 胡煦 Create on:2008-7-9
	 * @param companyId
	 * @return CompanyVO Edit author: Edit on: 修改原因：
	 */
	public CompanyVO findCompanyById(Long companyId) {
		super.checkParameterIsNull(companyId,
				"common:parameters_null_exception, ");
		return this.assembleD2V(manageAreaDao.findById(companyId));
	}

	/**
	 * 方法描述: 验证管理区是否存在楼栋
	 * @author 胡煦
	 * Create on:2008-10-16
	 * @param precinctId
	 * @return void
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private void checkPrecinctIsHas(Long precinctId){
		ChargePrecinctDomain chargePrecinctDomain = chargePrecinctDao.findById(precinctId);
		if(!BlankUtil.isBlank(chargePrecinctDomain.getBuildings())){
			throw new BusinessException("house:precinct_delete_reference_exception",
					BusinessException.WARNING);
		}
	}

	/**
	 * 方法描述: ChargePrecinctDomain to PrecinctVO
	 *
	 * @author 胡煦 Create on:2008-7-8
	 * @param ChargePrecinctDomain
	 * @return PrecinctVO Edit author: Edit on: 修改原因：
	 */
	@NoDACL
	private PrecinctVO assembleD2V(ChargePrecinctDomain chargePrecinctDomaint) {
		PrecinctVO precinctVO = new PrecinctVO();

		if (BlankUtil.isBlank(chargePrecinctDomaint)) {
			return precinctVO;
		}
		PopulateUtil.populate(chargePrecinctDomaint, precinctVO);
		PeriodDomain periodDomain = periodDao.queryCurrentPeriod();
		precinctVO.setPeriod(periodDomain==null?null:periodDomain.getPeriod());
		Set<BuildingDomain> buildingSet = new HashSet();

		buildingSet = chargePrecinctDomaint.getBuildings();
		StringBuffer buildingNames = new StringBuffer();
		StringBuffer buildingIds = new StringBuffer();
		for (BuildingDomain buildingDomain : buildingSet) {
			buildingNames.append(buildingDomain.getName());
			buildingNames.append(",");
			buildingIds.append(buildingDomain.getKeyword());
			buildingIds.append(",");
		}
		if (buildingNames.length() > 0) {
			buildingNames.deleteCharAt(buildingNames.length() - 1);
		}
		if (buildingIds.length() > 0) {
			buildingIds.deleteCharAt(buildingIds.length() - 1);
		}
		precinctVO.setBuildingIds(buildingIds.toString());
		precinctVO.setBuildingNames(buildingNames.toString());

		precinctVO
				.setManageOrgId(chargePrecinctDomaint.getManageOrg() == null ? null
						: chargePrecinctDomaint.getManageOrg().getKeyword());
		precinctVO.setPrecinctTypeId(sysrefcodeDao.getSysrefCodesByCode(
				SysrefcodeDO.HM_PRECINCT_TYPE_CODE,
				SysrefcodeDO.HM_PRECINCT_TYPE_VALUE).getId());
		precinctVO.setPrecinctTypeView(sysrefcodeDao.getSysrefCodesByCode(
				SysrefcodeDO.HM_PRECINCT_TYPE_CODE,
				SysrefcodeDO.HM_PRECINCT_TYPE_VALUE).getName());
		return precinctVO;
	}

	/**
	 *
	 * 功能描述: 得到所有已被管理区引用的楼栋
	 *
	 * @author 胡煦 Created on:2008-7-21
	 *
	 * 修改原因：
	 * @param
	 * @return List<BuildingDomain>
	 */
	public List<BuildingDomain> getBuildings() {

		List<BuildingDomain> resultBuildingList = new ArrayList<BuildingDomain>();
		List<ChargePrecinctDomain> resultList = new ArrayList<ChargePrecinctDomain>();
		resultList = chargePrecinctDao.findAll();

		for (ChargePrecinctDomain c : resultList) {
			Set<BuildingDomain> buildings = c.getBuildings();
			resultBuildingList.addAll(buildings);
		}

		return resultBuildingList;
	}

	/**
	 *
	 * 功能描述: 得到所有未被管理区引用的楼栋和被某个管理区引用的楼栋的集合
	 *
	 * @author 胡煦 Created on:2008-7-21
	 *
	 * 修改原因：
	 * @param
	 * @return List<BuildingDomain>
	 */
	public List<BuildingVO> getBuildingSet(Long precinctId) {
		List<BuildingVO> resultBuildingList = new ArrayList<BuildingVO>();
		resultBuildingList.addAll(getBuildingsBy(precinctId));
		resultBuildingList.addAll(getBuildingIdsNotUse());
		return resultBuildingList;
	}

	/**
	 *
	 * 功能描述: 得到被某个管理区引用的楼栋
	 *
	 * @author 胡煦 Created on:2008-7-21
	 *
	 * 修改原因：
	 * @param
	 * @return List<BuildingDomain>
	 */
	public List<BuildingVO> getBuildingsBy(Long precinctId) {
		List<BuildingVO> resultBuildingList = new ArrayList<BuildingVO>();
//		ChargePrecinctDomain chargePrecinctDomain = precinctDao
//				.findById(precinctId);
		List<BuildingDomain> buildings = buildingDao.getBuildingsByPrecinctId(precinctId);
		for (BuildingDomain buildingDomain : buildings) {
			resultBuildingList.add(buildingService.assembleD2V(buildingDomain));
		}
		return resultBuildingList;
	}

	/**
	 *
	 * 功能描述: 得到所有未被管理区引用的楼栋
	 *
	 * @author 胡煦 Created on:2008-7-21
	 *
	 * 修改原因：
	 * @param
	 * @return List<BuildingVO>
	 */
	public List<BuildingVO> getBuildingIdsNotUse() {
		List<BuildingDomain> buildingList = this.getBuildings();
		StringBuffer buildingIds = new StringBuffer();
		for (BuildingDomain buildingDomain : buildingList) {
			buildingIds.append(buildingDomain.getKeyword());
			buildingIds.append(",");
		}
		if (buildingIds.length() > 0) {
			buildingIds.deleteCharAt(buildingIds.length() - 1);
		}
		buildingList = buildingDao.getBuildings(buildingIds.toString(), true);
		List<BuildingVO> buildingVoList = new ArrayList<BuildingVO>();
		for (BuildingDomain buildingDomain : buildingList) {
			BuildingVO buildingVO = buildingService.assembleD2V(buildingDomain);
			buildingVoList.add(buildingVO);
		}
		return buildingVoList;
	}

	/**
	 * 方法描述: PrecinctVO to ChargePrecinctDomain
	 *
	 * @author 胡煦 Create on:2008-7-8
	 * @param PrecinctVO
	 * @return ChargePrecinctDomain Edit author: Edit on: 修改原因：
	 */
	private ChargePrecinctDomain assembleV2D(PrecinctVO precinctVO) {
		ChargePrecinctDomain chargePrecinctDomain = null;
		if (!BlankUtil.isBlank(precinctVO.getKeyword())) {
			chargePrecinctDomain = chargePrecinctDao
					.findById(precinctVO.getKeyword());
			chargePrecinctDomain.getBuildings().clear();
		} else {
			chargePrecinctDomain = new ChargePrecinctDomain();
		}
		if (BlankUtil.isBlank(precinctVO)) {
			return chargePrecinctDomain;
		}
		PopulateUtil.populate(precinctVO, chargePrecinctDomain);
		chargePrecinctDomain.setManageOrg(manageOrgDao.findById(precinctVO
				.getManageOrgId()));

		if (!BlankUtil.isBlank(precinctVO.getBuildingIds())) {
			String BuildingIds = precinctVO.getBuildingIds();
			chargePrecinctDomain.getBuildings().addAll(
					buildingDao.getBuildings(BuildingIds));
			chargePrecinctDomain
					.setHasSelected(SysrefcodeDO.CONSTANTS_DIGITAL_ONE);
		}
		chargePrecinctDomain
				.setHasSelected(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO);
		SysInfo sysInfo = createInfo(precinctVO.getCreator());
		chargePrecinctDomain.setSysInfo(sysInfo);
		if (!BlankUtil.isBlank(precinctVO.getModifier())) {
			this.modifyInfo(sysInfo, precinctVO.getModifier());
			chargePrecinctDomain.setSysInfo(sysInfo);
		}

		return chargePrecinctDomain;
	}

	/**
	 * 方法描述: CompanyDomain to CompanyVO
	 *
	 * @author 胡煦 Create on:2008-7-8
	 * @param CompanyDomain
	 * @return CompanyVO Edit author: Edit on: 修改原因：
	 */
	private CompanyVO assembleD2V(CompanyDomain companyDomain) {
		CompanyVO companyVO = new CompanyVO();

		if (BlankUtil.isBlank(companyDomain)) {
			return companyVO;
		}

		PopulateUtil.populate(companyDomain, companyVO);

		companyVO.setCreator(companyDomain.getSysInfo() == null ? null
				: companyDomain.getSysInfo().getCreator());
		companyVO.setModifier(companyDomain.getSysInfo() == null ? null
				: companyDomain.getSysInfo().getModifier());
		// companyVO.setQualifyCode(companyDomain.getQualify() == null ? null
		// :companyDomain.getQualify().getCode());
		// companyVO.setQualifyValue(companyDomain.getQualify()== null ? null
		// :companyDomain.getQualify().getValue());
		companyVO.setQualifyId(companyDomain.getQualify() == null ? null
				: companyDomain.getQualify().getId());
		companyVO.setQualifyViewValue(companyDomain.getQualify() == null ? null
				: companyDomain.getQualify().getName());

		return companyVO;
	}

	/**
	 * 方法描述: ManageOrgDomain to ManageOrgVO
	 *
	 * @author 胡煦 Create on:2008-7-8
	 * @param ManageOrgDomain
	 * @return ManageOrgVO Edit author: Edit on: 修改原因：
	 */
	private ManageOrgVO assembleD2V(ManageOrgDomain manageOrgDomain) {
		ManageOrgVO manageOrgVO = new ManageOrgVO();

		if (BlankUtil.isBlank(manageOrgDomain)) {
			return manageOrgVO;
		}

		PopulateUtil.populate(manageOrgDomain, manageOrgVO);

		manageOrgVO.setCreator(manageOrgDomain.getSysInfo() == null ? null
				: manageOrgDomain.getSysInfo().getCreator());
		manageOrgVO.setModifier(manageOrgDomain.getSysInfo() == null ? null
				: manageOrgDomain.getSysInfo().getModifier());
		manageOrgVO.setCompanyId(manageOrgDomain.getCompany() == null ? null
				: manageOrgDomain.getCompany().getKeyword());

		return manageOrgVO;
	}

	/**
	 * 方法描述: ManageOrgVO to ManageOrgDomain
	 *
	 * @author 胡煦 Create on:2008-7-8
	 * @param ManageOrgDomain
	 * @return ManageOrgVO Edit author: Edit on: 修改原因：
	 */
	private ManageOrgDomain assembleV2D(ManageOrgVO manageOrgVO) {
		ManageOrgDomain manageOrgDomain = new ManageOrgDomain();

		if (BlankUtil.isBlank(manageOrgVO)) {
			return manageOrgDomain;
		}

		PopulateUtil.populate(manageOrgVO, manageOrgDomain);
		SysInfo sysInfo = createInfo(manageOrgVO.getCreator());

		if (BlankUtil.isBlank(manageOrgVO.getModifier())) {
			modifyInfo(sysInfo, manageOrgVO.getModifier());
		}

		manageOrgDomain.setSysInfo(sysInfo);
		manageOrgDomain.setCompany(manageAreaDao.findById(manageOrgVO
				.getCompanyId()));
		return manageOrgDomain;
	}

	/**
	 * 方法描述: 得到所有物业公司及其下的管理处的树列表。 将每一个物业公司和其下的管理处列表放入一个Map中，再将Map添加进一个列表，用于返回。
	 *
	 * @author 付湘辉 Create on:2008-7-1
	 * @param
	 * @return List<Map> Edit author:胡煦 Edit on:2008-7-7 修改原因：使之适合物业公司节点树
	 */
	public List<Map> queryCompanyManageOrgTreeList() {
		List<Map> list = new ArrayList<Map>(1);
		Map<Object, List<ManageOrgVO>> companyManageOrgMap = new HashMap<Object, List<ManageOrgVO>>();

		// 得到所有物业公司信息
		List<CompanyDomain> companyList = manageAreaDao.findAll();

		CompanyVO CompanyVO = null;
		ManageOrgVO manageOrgVO = null;
		List<ManageOrgVO> manageOrgVOList = null;

		if (BlankUtil.isBlank(companyList)) {
			return list;
		}

		for (CompanyDomain domain : companyList) {
			companyManageOrgMap = new HashMap<Object, List<ManageOrgVO>>();

			CompanyVO = new CompanyVO();// this.assembleD2V(domain);
			CompanyVO.setKeyword(domain.getKeyword());
			CompanyVO.setName(domain.getName());
			// CompanyVO.setHasMoreProject(domain.getHasMoreProject());

			List<ManageOrgDomain> ManageOrgList = manageAreaDao
					.getAllManageOrgByCompanyId(domain.getKeyword());

			if (!BlankUtil.isBlank(ManageOrgList)) {

				manageOrgVOList = new ArrayList<ManageOrgVO>(ManageOrgList
						.size());

				for (ManageOrgDomain manageOrgDomain : ManageOrgList) {
					manageOrgVO = assembleD2V(manageOrgDomain);
					manageOrgVOList.add(manageOrgVO);
				}

				// ManageOrgVO已实现compareTo接口,添加自身逻辑,在比较时会用ManageOrgVO.seqNo进行比较,升序排列
				// Collections.sort(manageOrgVOList);
			}

			companyManageOrgMap.put(CompanyVO, manageOrgVOList);

			list.add(companyManageOrgMap);
		}

		return list;
		// return null;
	}

	/**
	 * 方法描述: 根据物业公司ID分页查询其下所有物业管理处
	 *
	 * @author 胡煦 Create on:2008-7-3
	 * @param companyId
	 * @param pageNo
	 * @param pageSize
	 * @return page Edit author: Edit on: 修改原因：
	 */
	public Page queryPageManageOrg(Long companyId, int pageNo, int pageSize) {
		super.checkParameterIsNull(companyId,
				"common:parameters_null_exception, ");
		Page page = manageAreaDao.queryPageManageOrg(companyId, pageNo,
				pageSize);

		List<ManageOrgDomain> list = page.getResult();
		List<ManageOrgVO> manageOrgVOList = new ArrayList<ManageOrgVO>();

		for (ManageOrgDomain manageOrgDomain : list) {
			ManageOrgVO ManageOrgVO = assembleD2V(manageOrgDomain);
			manageOrgVOList.add(ManageOrgVO);
		}

		page.setResult(manageOrgVOList);
		return page;
	}

	private IManageAreaDao manageAreaDao;

	private IManageOrgDao manageOrgDao;

	private IChargePrecinctDao chargePrecinctDao;

	private IBuildingDao buildingDao;

	private IBuildingService buildingService;

	private IPeriodDao periodDao;


	/**
	 * @param manageOrgDao
	 *            the manageOrgDao to set
	 */
	public void setManageOrgDao(IManageOrgDao manageOrgDao) {
		this.manageOrgDao = manageOrgDao;
	}

	/**
	 * @param manageAreaDao
	 *            the manageAreaDao to set
	 */
	public void setManageAreaDao(IManageAreaDao manageAreaDao) {
		this.manageAreaDao = manageAreaDao;
	}

	/**
	 * @param buildingDao
	 *            the buildingDao to set
	 */
	public void setBuildingDao(IBuildingDao buildingDao) {
		this.buildingDao = buildingDao;
	}

	/**
	 * @param buildingService
	 *            the buildingService to set
	 */
	public void setBuildingService(IBuildingService buildingService) {
		this.buildingService = buildingService;
	}

	/**
	 * @param periodDao the periodDao to set
	 */
	public void setPeriodDao(IPeriodDao periodDao) {
		this.periodDao = periodDao;
	}

	/**
	 * @param chargePrecinctDao the chargePrecinctDao to set
	 */
	public void setChargePrecinctDao(IChargePrecinctDao chargePrecinctDao) {
		this.chargePrecinctDao = chargePrecinctDao;
	}
}
